
const { ccclass } = cc._decorator;

@ccclass
export default class Rank extends cc.Component {

    display: cc.Sprite = null;
    tex: cc.Texture2D;
    hasCheckGroupRank: boolean = false;
    start() {
        this.node.active = false;

        this.display = this.node.getComponent(cc.Sprite);
        this.node.height = cc.find('Canvas').height;
        this.tex = new cc.Texture2D();

        //使用带shareTicket的转发  同时对主动转发和被动转发生效
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.updateShareMenu({
                withShareTicket: true
            })

            wx.onShow(res => {
                //判断是从群里 查看排行的 分享小卡片进入
                if (res.shareTicket && res.query.from == 'groupRank') {
                    this.showGroupRank(res.shareTicket);
                }
            })
        }
    }

    onReqGroupRank() {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.shareAppMessage({
                title: '群雄逐鹿，看看你第几',
                // imageUrl: ''
                query: 'from=groupRank'
            });
        }
    }

    showFriendRank() {
        this.node.active = true;
        if (this.node.active) {
            wx.postMessage({
                cmd: 'friend',
                uid: ''
            })
        }
    }

    showGroupRank(shareTicket) {
        this.node.active = true;
        wx.postMessage({
            cmd: 'group',
            shareTicket: shareTicket,
            uid: ''
        })
    }

    hide() {
        this.node.active = false;
        if (this.node.active) {
            wx.postMessage({
                cmd: 'hide',
            })
        }
    }

    update() {
        this._updaetSubDomainCanvas();
    }

    _updaetSubDomainCanvas() {
        if (!this.tex) {
            return;
        }
        var openDataContext = wx.getOpenDataContext();
        var sharedCanvas = openDataContext.canvas;
        this.tex.initWithElement(sharedCanvas);
        this.tex.handleLoadedTexture();

        this.display.spriteFrame = new cc.SpriteFrame(this.tex);
    }
}